package no83;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/5/17 13:48
 * 删除排序链表中的重复元素
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
 */
public class Solution {

    public ListNode deleteDuplicates(ListNode head) {
        // 边界处理
        if (head == null || head.next == null) {
            return head;
        }
        // 此时链表一定至少含有两个节点, 采用辅助指针的方式进行删除
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.val == cur.next.val) {
                // 存在重复元素, 进行删除操作
                cur.next = cur.next.next;
            } else {
                // 不存在重复元素, 则正常遍历
                cur = cur.next;
            }
        }
        return head;
    }


    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
